package com.farm.kaiping.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.farm.common.security.SecurityUtils;
import com.farm.kaiping.entity.MmChangeRecord;
import com.farm.kaiping.po.MmChangeRecordPO;
import com.farm.kaiping.mapper.MmChangeRecordMapper;
import com.farm.kaiping.service.MmChangeRecordService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.farm.common.util.StringRange;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 材料变更记录(MmChangeRecord)表服务实现类
 *
 * @author makejava
 * @since 2025-01-22 14:36:26
 */
@Service
@Slf4j
@AllArgsConstructor
public class MmChangeRecordServiceImpl extends ServiceImpl<MmChangeRecordMapper, MmChangeRecord> implements MmChangeRecordService {
    
	private LambdaQueryWrapper<MmChangeRecord> generateQueryWrapper(MmChangeRecordPO mmChangeRecordPO){
		LambdaQueryWrapper<MmChangeRecord> queryWrapper = new LambdaQueryWrapper<>();
		// _ID
		queryWrapper.eq(ObjectUtil.isNotEmpty(mmChangeRecordPO.getId()), MmChangeRecord::getId, mmChangeRecordPO.getId());
		queryWrapper.in(ObjectUtil.isNotEmpty(mmChangeRecordPO.getIdList()), MmChangeRecord::getId, mmChangeRecordPO.getIdList());
		// 材料的ID
		queryWrapper.eq(ObjectUtil.isNotEmpty(mmChangeRecordPO.getMaterialId()), MmChangeRecord::getMaterialId, mmChangeRecordPO.getMaterialId());
		// 变更类型(1入库2出库3盘亏4盘盈)
		queryWrapper.eq(ObjectUtil.isNotEmpty(mmChangeRecordPO.getChangeType()), MmChangeRecord::getChangeType, mmChangeRecordPO.getChangeType());
		// 变更类型(1入库2出库3盘亏4盘盈)
		queryWrapper.like(ObjectUtil.isNotEmpty(mmChangeRecordPO.getChangeTypeLabel()), MmChangeRecord::getChangeTypeLabel, mmChangeRecordPO.getChangeTypeLabel());
		// 变更数量
		queryWrapper.eq(ObjectUtil.isNotEmpty(mmChangeRecordPO.getChangeNum()), MmChangeRecord::getChangeNum, mmChangeRecordPO.getChangeNum());
		// 变更备注
		queryWrapper.like(ObjectUtil.isNotEmpty(mmChangeRecordPO.getChangeNote()), MmChangeRecord::getChangeNote, mmChangeRecordPO.getChangeNote());
		// 是否删除(0否1是)
		queryWrapper.eq(ObjectUtil.isNotEmpty(mmChangeRecordPO.getDelFlag()), MmChangeRecord::getDelFlag, mmChangeRecordPO.getDelFlag());
        queryWrapper.eq(ObjectUtil.isEmpty(mmChangeRecordPO.getDelFlag()), MmChangeRecord::getDelFlag, 0);
		// 新增时间
		queryWrapper.eq(ObjectUtil.isNotEmpty(mmChangeRecordPO.getCreateTime()), MmChangeRecord::getCreateTime, mmChangeRecordPO.getCreateTime());
		// 创建人ID
		queryWrapper.eq(ObjectUtil.isNotEmpty(mmChangeRecordPO.getCreateUserId()), MmChangeRecord::getCreateUserId, mmChangeRecordPO.getCreateUserId());
		// 创建人姓名
		queryWrapper.like(ObjectUtil.isNotEmpty(mmChangeRecordPO.getCreateUserName()), MmChangeRecord::getCreateUserName, mmChangeRecordPO.getCreateUserName());
		// 更新时间
		queryWrapper.eq(ObjectUtil.isNotEmpty(mmChangeRecordPO.getUpdateTime()), MmChangeRecord::getUpdateTime, mmChangeRecordPO.getUpdateTime());
		// 更新人ID
		queryWrapper.eq(ObjectUtil.isNotEmpty(mmChangeRecordPO.getUpdateUserId()), MmChangeRecord::getUpdateUserId, mmChangeRecordPO.getUpdateUserId());
		// 更新人姓名
		queryWrapper.like(ObjectUtil.isNotEmpty(mmChangeRecordPO.getUpdateUserName()), MmChangeRecord::getUpdateUserName, mmChangeRecordPO.getUpdateUserName());
		// 租户ID
		queryWrapper.eq(ObjectUtil.isNotEmpty(mmChangeRecordPO.getTenantId()), MmChangeRecord::getTenantId, mmChangeRecordPO.getTenantId());
		// 创建时间时间区间查询
		queryWrapper.ge(ObjectUtil.isNotEmpty(StringRange.getOne(mmChangeRecordPO.getCreateTimeBetween(),0)), MmChangeRecord::getCreateTime,StringRange.getOne(mmChangeRecordPO.getCreateTimeBetween(),0));
		queryWrapper.le(ObjectUtil.isNotEmpty(StringRange.getOne(mmChangeRecordPO.getCreateTimeBetween(),1)), MmChangeRecord::getCreateTime,StringRange.getOne(mmChangeRecordPO.getCreateTimeBetween(),1));
		// 更新时间时间区间查询
		queryWrapper.ge(ObjectUtil.isNotEmpty(StringRange.getOne(mmChangeRecordPO.getUpdateTimeBetween(),0)), MmChangeRecord::getUpdateTime,StringRange.getOne(mmChangeRecordPO.getUpdateTimeBetween(),0));
		queryWrapper.le(ObjectUtil.isNotEmpty(StringRange.getOne(mmChangeRecordPO.getUpdateTimeBetween(),1)), MmChangeRecord::getUpdateTime,StringRange.getOne(mmChangeRecordPO.getUpdateTimeBetween(),1));

		queryWrapper.orderByDesc(MmChangeRecord::getCreateTime,MmChangeRecord::getId);
		return queryWrapper;
	}

	 /**
	  * @describe 分页查询材料变更记录
	  * @author makejava
	  * @date 2025-01-22 14:36:26
	  */
	@Override
	public Page<MmChangeRecord> getMmChangeRecordPage(Page page, MmChangeRecordPO mmChangeRecordPO){
		LambdaQueryWrapper<MmChangeRecord> queryWrapper = this.generateQueryWrapper(mmChangeRecordPO);
        return this.baseMapper.selectPage(page, queryWrapper);
	}
	
	/**
     * @describe 查询材料变更记录list
     * @author  makejava
     * @date  2025-01-22 14:36:26
     */
	@Override
	public List<MmChangeRecord> getMmChangeRecordList(MmChangeRecordPO mmChangeRecordPO){
		LambdaQueryWrapper<MmChangeRecord> queryWrapper = this.generateQueryWrapper(mmChangeRecordPO);
        return this.baseMapper.selectList(queryWrapper);
	}
	
	/**
     * @describe 获取一条数据 材料变更记录
     * @author  makejava
     * @date  2025-01-22 14:36:26
     */
	@Override
	public MmChangeRecord getMmChangeRecord(MmChangeRecordPO mmChangeRecordPO){
		LambdaQueryWrapper<MmChangeRecord> queryWrapper = this.generateQueryWrapper(mmChangeRecordPO);
        queryWrapper.last("LIMIT 1");
        return this.baseMapper.selectOne(queryWrapper);
	}

    /**
     * @describe 新增材料变更记录
     * @author  makejava
     * @date  2025-01-22 14:36:26
     */
	@Override
	public Integer saveMmChangeRecord(MmChangeRecordPO mmChangeRecordPO) {
		if(ObjectUtil.isEmpty(mmChangeRecordPO.getMaterialId())){
			throw new RuntimeException("材料的ID为空");
		}
		if(ObjectUtil.isEmpty(mmChangeRecordPO.getChangeType())){
			throw new RuntimeException("变更类型(1入库2出库3盘亏4盘盈)为空");
		}
		if(ObjectUtil.isEmpty(mmChangeRecordPO.getChangeTypeLabel())){
			throw new RuntimeException("变更类型(1入库2出库3盘亏4盘盈)为空");
		}
		if(ObjectUtil.isEmpty(mmChangeRecordPO.getChangeNum())){
			throw new RuntimeException("变更数量为空");
		}
		if(ObjectUtil.isEmpty(mmChangeRecordPO.getChangeNote())){
			throw new RuntimeException("变更备注为空");
		}
        mmChangeRecordPO.setDelFlag(0);
		mmChangeRecordPO.setTenantId(SecurityUtils.getUser().getTenantId());// 租户ID
		mmChangeRecordPO.setCreateTime(LocalDateTime.now());
		mmChangeRecordPO.setCreateUserId(SecurityUtils.getUser().getUserId());// 创建人
		mmChangeRecordPO.setCreateUserName(SecurityUtils.getUser().getRealName());// 创建人
		mmChangeRecordPO.setUpdateTime(LocalDateTime.now());
		MmChangeRecord mmChangeRecord = BeanUtil.copyProperties(mmChangeRecordPO, MmChangeRecord.class);
		this.save(mmChangeRecord);
		return mmChangeRecord.getId();
	}

	/**
     * @describe 修改材料变更记录
     * @author  makejava
     * @date  2025-01-22 14:36:26
     */
	@Override
	public String updateMmChangeRecord(MmChangeRecordPO mmChangeRecordPO) {
		if (ObjectUtil.isEmpty(mmChangeRecordPO.getId())) {
			throw new RuntimeException("id为空");
		}

		mmChangeRecordPO.setUpdateUserId(SecurityUtils.getUser().getUserId());// 更新人
		mmChangeRecordPO.setUpdateUserName(SecurityUtils.getUser().getRealName());// 更新人
		mmChangeRecordPO.setUpdateTime(LocalDateTime.now());
		MmChangeRecord mmChangeRecord = BeanUtil.copyProperties(mmChangeRecordPO, MmChangeRecord.class);
		this.updateById(mmChangeRecord);
		return "修改成功";
	}

	/**
     * @describe 通过id删除材料变更记录
     * @author  makejava
     * @date  2025-01-22 14:36:26
     */
	@Override
	public String removeMmChangeRecord(MmChangeRecordPO mmChangeRecordPO) {
		if (ObjectUtil.isEmpty(mmChangeRecordPO.getBatchDel())) {
			throw new RuntimeException("删除数据未选择");
		}
		LambdaUpdateWrapper<MmChangeRecord> removeWrapper = new LambdaUpdateWrapper<>();
		removeWrapper.in(MmChangeRecord::getId, mmChangeRecordPO.getBatchDel());
		removeWrapper.set(MmChangeRecord::getDelFlag,1);
		removeWrapper.set(MmChangeRecord::getUpdateUserId,SecurityUtils.getUser().getUserId());// 更新人
		removeWrapper.set(MmChangeRecord::getUpdateUserName,SecurityUtils.getUser().getRealName());// 更新人
		removeWrapper.set(MmChangeRecord::getUpdateTime,LocalDateTime.now());

		this.update(removeWrapper);
		return "删除成功";
	}
	
}

